
$(function() {
	loadFollowQuestionList();
})

function loadFollowQuestionList() {
	$('#followQuestionListTable').bootstrapTable({
		method : 'post',
		url : "getFollowQuestionListData",
		height : $(window).height() - 100,
		striped : true,
		minimumCountColumns : 2,
		smartDisplay : true,
		toolbar : "#followQuestionListTableToolbar",
		pagination : true,
		sidePagination : "server",
		pageNumber : 1,
		pageSize : 10,
		pageList : [ 10, 20, 30 ],
		queryParamsType : "limit",
		queryParams:queryParams,
		contentType : "application/x-www-form-urlencoded",
		columns : [{
			field : 'id',
			title : 'ID',
			visible : false
		}, {
			title : '序号',
			align : 'center',
			width : '3%',
			formatter : serilNumFormatter
		},  {
			field : 'title',
			title : '名称',
			align : 'center',
			width : '8%'
		}, {
			field : 'questionTitle',
			title : '标题',
			align : 'center',
			width : '15%'
		},{
			field : 'content',
			title : '描述',
			align : 'center',
			width : '30%'
		}, {
			field : 'status',
			title : '状态',
			align : 'center',
			width : '5%'
		}, {
			field : 'sort',
			title : '排序',
			align : 'center',  
			width : '5%',
			searchable : true,
			editable : {
				type : 'text',
				title : '修改排序编码',
				validate : function(sort) {
					if(sort==""||isNaN(sort))//isNaN() 函数用于检查其参数是否是非数字值。
					{
						return "请填写数字";
					}
					var data = $('#followQuestionListTable').bootstrapTable('getData'), index = $(this).parents('tr').data('index');
					
					if(data[index].status !='正常' && data[index].status !=''){//1正常 -1删除
						return '只有状态为"正常"才可以修改';
					}
					sort = $.trim(sort);
					if (!sort) {
						return '该项必填';
					}

					var info = '';

					$.post("getStatus", {
						id : data[index].id,
						sort : sort
					}, function(data) {
						
						if (!data.success) {
							info = data.msg;
						}else{
							$.success("操作成功", function() {
								search();
							});
						}
					}, "json");
					return info;
				}
			}
		},{
			title : '操作',
			align : 'center',
			width : '10%',
			valign : 'middle',
			formatter : operateFormatter
		}]
	});

}

function search() {
	$('#followQuestionListTable').bootstrapTable('destroy');
	loadFollowQuestionList();
}

function queryParams(params) {
	return {
		title :$("input[name='title']").val(),
		status:$("select[name='status']").val(),
		questionCategoryId:$("select[name='questionCategoryId']").val(),
		questionTitle:$("input[name='questionTitle']").val(),
		
		limit:params.limit,
		offset:params.offset
	}
}

function operateFormatter(value, row, index) {
	if (row.status =='删除') { 
		return[
		       '<i>已删除</i>',
		       ].join('');
	}
	return [
	        	'<a class="search"  href="getFollowQuestionView?id='+row.id+'" title="编辑"><i class="glyphicon glyphicon-edit">编辑</i></a>',
	        	'<a class="remove" href="javascript:void(0)" onClick="del('+ index +')" title="删除"><i class="glyphicon glyphicon-remove">删除</i></a>'
	        	]
			.join('');
}

function del(index) {
	var data = $('#followQuestionListTable').bootstrapTable('getData');
	var id = data[index].id;
	var sta = data[index].status;
	var status;
	if(sta == '正常'){
		status = 1;
	}else{
		status = -1;
	}
	layer.confirm("确认删除吗?", {
		btn : [ '确定', '取消' ]
	}, function() {
		$.getJSON("getStatus", {
			id : id,
			status : -1
		}, function(data) {
			if (data.success) {
				$.success("操作成功", function() {
					search();
				});
			}else{
				$.error(data.msg);
			}

		});
	}, function() {
		$.warn("您取消了操作");
	});
}



